#include <mongoose.h>
#include <assert.h>
#include <stdlib.h>

struct mg_server* server = NULL;

int mg_handler(struct mg_connection* conn, enum mg_event ev) {
  switch (ev) {
    case MG_AUTH:
      return MG_TRUE;

    case MG_REQUEST:
      conn->connection_param = ::malloc(100);
      mg_send_status(conn, 200);
      mg_printf_data(conn, "%s", "ok\n");
      return MG_TRUE;

    case MG_CLOSE:
      printf("close with connection_param: %p\n", conn->connection_param);
      return MG_TRUE;

    default:
      return MG_FALSE;
  }
}

int main() {
  server = ::mg_create_server(NULL, &mg_handler);

  ::mg_set_option(server, "listening_port", "3322");

  while (true) {
    if (::mg_poll_server(server, 1000) < 0) break;
  }

  return 0;
}
